/* 
 * Copyright (C), 2016-2020, 厦门盈欣宝金融信息服务有限公司
 * Project Name :boot-parent
 * File Name: org.easytouseit.redis.controller
 * Encoding UTF-8 
 * Version: 1.0 
 * Date: 2018-09-12 15:40
 * History:  
 * 1. Date: 2018-09-12
 *    Author: guojm 
 *    Modification: 新建
 * 2. ...
 */
package org.easytouseit.redis.controller;

import com.alibaba.fastjson.JSONObject;
import org.easytouseit.redis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * @author guojm
 * @version V1.0
 * @date 2018-09-12 下午 15:40
 */
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    RedisTemplate redisTemplate;
    @GetMapping("getUser")
    public String getUser() {
        return userService.getUser(001);
    }
    @GetMapping("search")
    public String search(){
        Set<String> keys = redisTemplate.keys("*");
        StringBuilder stringBuilder = new StringBuilder();
        if(!CollectionUtils.isEmpty(keys)){
            for (String key:keys){
                stringBuilder.append(key).append(":").append(JSONObject.toJSONString(redisTemplate.opsForValue().get(key)));
            }
        }
      return stringBuilder.toString();
    }
     @RequestMapping("deleteAll")
    public JSONObject deleteAll(){
        JSONObject jsonObject = new JSONObject();
        Set<String> keys = redisTemplate.keys("*");
        if(!CollectionUtils.isEmpty(keys)){
            for (String key:keys){
                redisTemplate.delete(key);
            }
        }
         jsonObject.put("code","1");
         jsonObject.put("msg","删除成功");
         return jsonObject;
    }

}
